package com.me.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.me.dao.StudentPaperDAO;
import com.me.entity.StudentPaper;

@Service
public class StudentPaperService extends BaseService<StudentPaper, StudentPaperDAO> {
	/**
	 * 注入持久层对象
	 */
	@Resource
	private StudentPaperDAO studentPaperDAO;
	
	@Override
	protected StudentPaperDAO getDao() {
		return studentPaperDAO;
	}
	
	/**
	 * 判断学生账号是否存在
	 */
	public boolean isExistStudent(StudentPaper studentPaper){
		Assert.notNull(studentPaper);
		if(studentPaper.getAccount() == null || "".equals(studentPaper.getAccount())){
			return false;
		}
		StudentPaper temp = new StudentPaper();
		temp.setAccount(studentPaper.getAccount());
		if(studentPaperDAO.preciseCount(temp) > 0){
			return true;
		}
		return false;
	}

	/**
	 * 校验用户名和密码是否正确
	 */
	public boolean checkLogin(StudentPaper studentPaper){
		Assert.notNull(studentPaper);
		if(studentPaper.getAccount() == null || "".equals(studentPaper.getAccount())){
			return false;
		}
		if(studentPaper.getPassword() == null || "".equals(studentPaper.getPassword())){
			return false;
		}
		if(studentPaperDAO.preciseCount(studentPaper) > 0){
			return true;
		}
		return false;
	}
	
	/**
	 * 获取试卷状态
	 */
	public Integer getPaperStatus(StudentPaper studentPaper){
		Assert.notNull(studentPaper);
		Assert.hasText(studentPaper.getAccount());
		studentPaper = studentPaperDAO.queryByAccount(studentPaper.getAccount());
		return studentPaper.getPaperState();
	}
	
	/**
	 * 根据试卷编号获取试卷的选择题题号串
	 */
	public String getPaperChoiceIds(Integer id){
		Assert.notNull(id);
		StudentPaper studentPaper = studentPaperDAO.queryById(id);
		return studentPaper.getChoiceId();
	}
	
	/**
	 * 根据试卷编号获取试卷的选择题正确答案串
	 */
	public String getPaperChoiceAnswers(Integer id){
		Assert.notNull(id);
		StudentPaper studentPaper = studentPaperDAO.queryById(id);
		return studentPaper.getChoiceAccurateAnswer();
	}
	
	/**
	 * 根据账号来更新信息
	 */
	public void updateByAccount(StudentPaper studentPaper){
		Assert.notNull(studentPaper);
		Assert.hasText(studentPaper.getAccount());
		studentPaperDAO.updateByAccount(studentPaper);
	}
	
	/**
	 * 根据账号来查找信息
	 * @param studentPaper
	 * @return
	 */
	public StudentPaper queryByAccount(StudentPaper studentPaper){
		Assert.notNull(studentPaper);
		Assert.hasText(studentPaper.getAccount());
		return studentPaperDAO.queryByAccount(studentPaper.getAccount());
	}
}
